package com.yonyou.common.service.messagecenter.rabbitmq;

import java.io.IOException;
import java.util.Map;
import java.util.concurrent.TimeoutException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.client.RestTemplate;

import com.yonyou.common.service.messagecenter.dao.TtMessageHistoryMapper;
import com.yonyou.common.service.messagecenter.util.MqConsts;
import com.yonyou.common.service.messagecenter.util.HttpClientUtil;
import com.yonyou.common.service.messagecenter.util.HttpUtil;

import net.sf.json.JSONObject;

public class UrlConsumer extends MessageConsumer{
	
	
	private Logger logger = LoggerFactory.getLogger(this.getClass());
	
	private String url;
	
	private RestTemplate restTemplate = new RestTemplate();
	
	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public UrlConsumer() throws IOException, TimeoutException {
		super("");
		// TODO Auto-generated constructor stub
	}

	public UrlConsumer(String endPointName) throws IOException, TimeoutException {
		super(endPointName);
		// TODO Auto-generated constructor stub
	}

	@Override
	public boolean listen(String protocol,Map<String,String> result) {
		String data="";
		try{
			logger.info(this.endPointName+"-----messsage do ok,,-----"+protocol.toString());
			System.out.println(this.endPointName+"-----messsage do ok,,-----"+protocol.toString());
			//restTemplate.postForLocation(url,protocol);
			String ct="";
			if(this.getDataMapping()!=null){
				protocol=this.getDataMapping().convert(protocol);
				ct=this.getDataMapping().getContentType();
			}
			if((ct==null) || "".equals(ct)){
				data=HttpUtil.postContainSsl(url,protocol );
			}else{
				data=HttpUtil.postContainSsl(url,protocol,ct);
				//data=HttpClientUtil.sendPostRequest(url, protocol, ct);
			}
    		result.put(MqConsts.RESPONSE_DATA_MESSAGE, data);
			
    		int b=0,e=data.length();
    		if(e>0){
            	if(String.valueOf(data.charAt(0)).equals("["))
            		b=1;
            	if(String.valueOf(data.charAt(data.length()-1)).equals("]"))
            		e=e-1;
            	data=data.substring(b, e);
    		}
    		
    		if(data.indexOf("result:\"200\"")>=0){
    			logger.info("消息下游接收成功，返回结果"+data);
			    return true;
    		}else if(data.indexOf("\"result\":200")>=0){
                logger.info("消息下游接收成功，返回结果"+data);
                return true;
    		}else if(data.indexOf("\"result\":\"200\"")>=0){
                logger.info("消息下游接收成功，返回结果"+data);
                return true;
            }else{
    			logger.warn("未成功消费");
    		}
            
    		
//    		String id="";
//    		String data1=data.substring(0, 1);
//    		if(data1.equals("{")){
//    			JSONObject obj=JSONObject.fromObject(data);
//    			id=(String)obj.get(MqConsts.RESPONSE_DATA_CODE);
//    		}
//			if(id.equals(MqConsts.RESPONSE_DATA_VALUE_SUCCESS)){
//				logger.info("消息下游接收成功，返回结果"+data);
//			    return true;
//			}else{
//				logger.warn("消息下游接收完成，但是没有返回200，返回结果"+data);
//			}
		}catch(Exception e){
			logger.error("消息消费失败",e);
			e.printStackTrace();
    		result.put(MqConsts.RESPONSE_DATA_MESSAGE, e.getMessage()+",data="+data);
		}
		return false;
	}

}
